package com.android.justcook;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.ActionBar;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import camp.android.justcook.R;

public class FinishedActivity extends BaseActivity {
	
	private final int TAKE_PICTURE = 1;
	private Uri imageUri;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		setContentView(R.layout.activity_finished);

		ActionBar actionBar = getActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);
		
		int REC_IMAGE = MainActivity.MEALS_IMAGES[getIntent().getIntExtra("CATEGORY_ID",0)][getIntent().getIntExtra("rec_id",0)];
		ImageView img = (ImageView) findViewById(R.id.imageView1);
		img.setBackgroundResource(REC_IMAGE);
		
		String recipe_name = MainActivity.MEALS[getIntent().getIntExtra("CATEGORY_ID",0)][getIntent().getIntExtra("rec_id",0)];
		SharedPreferences prefs = getSharedPreferences(PREFS_NAME,0);
		int progress = prefs.getInt(POINTS, 0);
		
		String end_text = "Congratulations on finishing " + recipe_name + "!\n You now have " + progress + " points."; 
		TextView text = (TextView) findViewById(R.id.textView1);
		text.setText(end_text);
		
		TextView cover_text = (TextView) findViewById(R.id.cover_category);
		cover_text.setText(recipe_name);
		
		super.onCreate(savedInstanceState);
	}

	public void takePhoto(View view) {
		Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
		File photo = new File(Environment.getExternalStorageDirectory(),
				"Pic.jpg");
		intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
		imageUri = Uri.fromFile(photo);
		startActivityForResult(intent, TAKE_PICTURE);
	}

	public void sharePhoto(View view) {

		Intent share = new Intent(Intent.ACTION_SEND);
		share.setType("image/jpeg");
		ByteArrayOutputStream bytes = new ByteArrayOutputStream();
		File f = new File(Environment.getExternalStorageDirectory()
				+ File.separator + "Pic.jpg");
		try {
			f.createNewFile();
			FileOutputStream fo = new FileOutputStream(f);
			fo.write(bytes.toByteArray());
		} catch (IOException e) {
			e.printStackTrace();
		}
		share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/Pic.jpg"));
		startActivity(Intent.createChooser(share, "Share Image"));

	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case TAKE_PICTURE:
			if (resultCode == Activity.RESULT_OK) {

				Uri selectedImage = imageUri;
				getContentResolver().notifyChange(selectedImage, null);
				ImageView imageView = (ImageView) findViewById(R.id.imageView);
				ContentResolver cr = getContentResolver();
				Bitmap bitmap;
				try {
					bitmap = android.provider.MediaStore.Images.Media
							.getBitmap(cr, selectedImage);


					imageView.setImageBitmap(bitmap);
					Toast.makeText(this, selectedImage.toString(),
							Toast.LENGTH_LONG).show();
				} catch (Exception e) {
					Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
							.show();
					Log.e("Camera", e.toString());
				}
			}
		}
	}

}
